package operations;

import book.BookList;
import tools.BinarySearch;
import tools.NameComparator;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA
 * Description:归还图书
 * User:22954
 * Date:2024-01-12
 * Time:20:36
 */
public class ReturnOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("归还图书");
        NameComparator nameComparator = new NameComparator();
        bookList.getBooks().sort(nameComparator);
        System.out.print("Please enter the name of the book you want to return:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        BinarySearch binarySearch = new BinarySearch();
        int index = binarySearch.binarytpSearch(bookList.getBooks(), name);
        if (index >= 0) {
            if (bookList.getBooks().get(index).isBorrowed()) {
                bookList.getBooks().get(index).setBorrowed(false);
                System.out.println("Successful restitution!");
                return;
            }
        }
        System.out.println("This book is not available!");
    }
}
